package com.ruicar.afs.cloud.product.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.product.entity.WorkTaskAssign;
import com.ruicar.afs.cloud.product.entity.WorkTaskCreated;
import com.ruicar.afs.cloud.product.enums.WorkflowTypeEnum;
import com.ruicar.afs.cloud.product.service.WorkTaskAssignService;
import com.ruicar.afs.cloud.product.service.WorkTaskCreatedService;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.TaskAssignRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.TaskCreateRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.UpdateBizDataRequest;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.response.TaskAssignResponse;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.response.TaskCreateResponse;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.response.UpdateBizDataResponse;
import com.ruicar.afs.cloud.workflow.gwt.processor.AfsWorkFlowNoticeProcessor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author limingzhi
 */
@Component
@Slf4j
@AllArgsConstructor
public class ChannelOnlineNoticeProcessor implements AfsWorkFlowNoticeProcessor {
    private final WorkTaskCreatedService workTaskCreatedService;
    private final WorkTaskAssignService workTaskAssignService;

    /**
     * 当前实现提供给哪些流程使用
     *
     * @return
     */
    @Override
    public List<String> afsFLowKeys() {
        //渠道上线：新车，二手车
        return Arrays.asList(new String[]{
                AfsEnumUtil.key(WorkflowTypeEnum.PRODUCT_LAUNCH)
                , AfsEnumUtil.key(WorkflowTypeEnum.PRODUCT_MODIFY)
        });
    }

    @Override
    public TaskCreateResponse taskCreated(TaskCreateRequest taskCreateRequest) {
        //保存节点业务数据
        WorkTaskCreated workTaskCreated = workTaskCreatedService.getOne(Wrappers.<WorkTaskCreated>query().lambda().eq(WorkTaskCreated::getBizDataId, taskCreateRequest.getBizDataId()));
        if (workTaskCreated == null) {
            WorkTaskCreated created = new WorkTaskCreated();
            BeanUtils.copyProperties(taskCreateRequest, created);
            workTaskCreatedService.save(created);
        } else {
            BeanUtils.copyProperties(taskCreateRequest, workTaskCreated);
            workTaskCreatedService.updateById(workTaskCreated);
        }

        log.info("接收到任务创建通知(节点业务参数):{}", JSON.toJSONString(taskCreateRequest));
        TaskCreateResponse response = new TaskCreateResponse();
        response.setSuccess(true);
        return response;
    }

    @Override
    public TaskAssignResponse taskAssigned(TaskAssignRequest taskAssignRequest) {
        //保存代办任务信息
        WorkTaskAssign assign = new WorkTaskAssign();
        BeanUtils.copyProperties(taskAssignRequest, assign);
        assign.setAssignId(taskAssignRequest.getId());
        assign.setId(null);
        workTaskAssignService.save(assign);
        log.info("接收到任务分配通知(代办任务处理人):{}", JSON.toJSONString(taskAssignRequest));
        TaskAssignResponse response = new TaskAssignResponse();
        response.setSuccess(true);
        return response;
    }

    @Override
    public UpdateBizDataResponse updateBizData(UpdateBizDataRequest updateBizDataRequest) {
        log.info("接收到更新业务通知:{}", JSON.toJSONString(updateBizDataRequest));
        UpdateBizDataResponse response = new UpdateBizDataResponse();
        response.setSuccess(true);
        return response;
    }
}
